! This little program uses Word 97 as a report writer

! DATA SECTION
LOC:DocumentObject      CSTRING(35)
LOC:AppObject           CSTRING(35)
LOC:RangeObject         CSTRING(35)
LOC:MergeProgressString CSTRING(75)

QuickWindow          WINDOW('Test OLE'),AT(0,0,359,186),FONT('MS Sans Serif',8,,),IMM,SYSTEM,GRAY,RESIZE,MDI
                       STRING(@s74),AT(4,171,238,10),USE(LOC:MergeProgressString),HIDE,FONT(,,,FONT:bold)
                       OLE,AT(0,0,0,0),USE(?OLE),COMPATIBILITY(020H)
                       END
                     END

! CODE SECTION

            DO CheckOLEObject
            LOC:DocumentObject = ?OLE{'Documents.Add'}		! Create a blank document
            ?OLE{'ActiveDocument.PageSetup.TogglePortrait'}	! Set it to portrait
            CLEAR(MAT:Record)					! LOOP through a file
            SET(MAT:FileRefKey,MAT:FileRefKey)			! In this case, my Matter file
            LOOP UNTIL Access:Matter.Next()
                 LOC:RangeObject = ?OLE{'ActiveDocument.Range(0, 0)'}	! Must have a range object so we can use the 
									! InsertAfter method

                 ?OLE{LOC:RangeObject & '.InsertAfter("' & MAT:FileRef & '<09>' &  MAT:Description & '<13>' & '")'}	! Add each record with TABS in between the fields
															! and a Return after each record. VOILA! A simple list!
            END
            ?OLE{LOC:DocumentObject & '.SaveAs("C:\TEMP\REPORT.TXT",3)'}	! Save as a text file
            ?OLE{'Visible'} = -1						! Make Word visible (Note: True = -1 in Word Basic!!!)
            ?OLE{LOC:AppObject & '.Activate'}					! Make it active

            ! The user can obviously print it out (from Word) now but if you want to
            ! avoid the overload of the entire Microsoft Word Object
            ! and print it out automatically, just issue this command in place
	    ! of the three lines above
	
	    ?OLE('ActiveDocument.PrintOut'}		! Note there is a bug in in C5OLEX.DLL
							! which may give you a "Parameter 0 Type Mismatch" error here
							! Contact Topspeed for the patch or check Icetips or (as a last
							! resort e-mail me at rjordan@mweb.co.za)

		



CheckOLEObject  ROUTINE

! Because the user can shut down the instance of Word 97 that the
! program might be using I wrote this routine to check if we need
! to create a new instance of Word or whether the program's instance
! is still active before sending any OLE commands.
! This routine must be executed at least once anyway (at the beginning)


   ?OLE{PROP:ReportException} = True	! Turn this off in the final program
					! It displays any OLE errors

   LOC:AppObject = ?OLE{'Application'}	! Will return the Application Object ID if it is still active
					! else will return 'GetPropertyFailed' OR 'No ole automation interface'

   IF LOC:AppObject = 'GetPropertyFailed' OR LOC:AppObject = 'No ole automation interface'
      LOC:MergeProgressString = 'Loading Word 97';DISPLAY(?LOC:MergeProgressString)
      ?OLE{PROP:Create} = 'Word.Application.8'
      LOC:AppObject = ?OLE{'Application'}
      ?OLE{'ChangeFileOpenDirectory("' & EMP:MergedDocumentLocation & '")'} ! I change the default directory here (not essential)
   ELSE
      LOC:MergeProgressString = 'Using existing Word 97';DISPLAY(?LOC:MergeProgressString)
   END



